package com.datasure.domain.entity;

import com.datasure.domain.entity.base.Entity;
import com.datasure.domain.service.SourceDeviceDomainService;
import com.datasure.types.IntegerId;
import com.datasure.types.StringId;
import lombok.*;

import javax.xml.bind.ValidationException;
import java.util.Date;
import java.util.Objects;

/**
 * @author zhangxiaolong
 * @ClassName Device.java
 * @Description
 * @createTime 2022年01月13日 15:04:00
 */
@Builder
@Data
@Setter(value = AccessLevel.PRIVATE)
public class Device implements Entity<IntegerId> {
    @Override
    public IntegerId getId() {
        return deviceId;
    }

    private IntegerId deviceId;

    private StringId deviceUniqueId;

    private Integer deviceState;

    private Integer deviceMarkDel;

    private String deviceMac;

    private String deviceIp;

    private String deviceClientComputerName;

    private Integer deviceIsStandby;

    private Integer deviceStandbyId;

    private Integer deviceLicenseFlag;

    private Integer deviceClientSystype;

    private String deviceClientSysversion;

    private String deviceClientVersion;

    private Integer deviceClientRegister;

    private Date deviceClientSteupDateTime;

    private String deviceClientWorkgroup;

    private String fcConnectionStr;

    private String iscsiConnectionStr;

    private Date offlineTime;

    private Integer deviceSortIndex;

    private String deviceRemark;

    private Integer deviceStandbyModel;

    private Date createTime;

    private Date updateTime;

    private String deviceDescription;

    private Integer devicType;

    private Integer deviceIsNew;

    private String clSqS;

    private Integer deviceClientUpgradeState;

    private Integer deviceClusterIdentity;

    private String deviceHardInfo;

    private Integer devicePort;

    private Integer concurrencyCount;

    private Integer deviceInstallType;

    private String targetclientUrl;

    private Integer deviceInstallId;

    public Device(IntegerId deviceId, StringId deviceUniqueId, Integer deviceState, Integer deviceMarkDel, String deviceMac, String deviceIp, String deviceClientComputerName, Integer deviceIsStandby, Integer deviceStandbyId, Integer deviceLicenseFlag, Integer deviceClientSystype, String deviceClientSysversion, String deviceClientVersion, Integer deviceClientRegister, Date deviceClientSteupDateTime, String deviceClientWorkgroup, String fcConnectionStr, String iscsiConnectionStr, Date offlineTime, Integer deviceSortIndex, String deviceRemark, Integer deviceStandbyModel, Date createTime, Date updateTime, String deviceDescription, Integer devicType, Integer deviceIsNew, String clSqS, Integer deviceClientUpgradeState, Integer deviceClusterIdentity, String deviceHardInfo, Integer devicePort, Integer concurrencyCount, Integer deviceInstallType, String targetclientUrl, Integer deviceInstallId) {
        this.deviceId = deviceId;


        this.deviceUniqueId = deviceUniqueId;
        this.deviceState = deviceState;
        this.deviceMarkDel = deviceMarkDel;
        this.deviceMac = deviceMac;
        this.deviceIp = deviceIp;
        this.deviceClientComputerName = deviceClientComputerName;
        this.deviceIsStandby = deviceIsStandby;
        this.deviceStandbyId = deviceStandbyId;
        this.deviceLicenseFlag = deviceLicenseFlag;
        this.deviceClientSystype = deviceClientSystype;
        this.deviceClientSysversion = deviceClientSysversion;
        this.deviceClientVersion = deviceClientVersion;
        this.deviceClientRegister = deviceClientRegister;
        this.deviceClientSteupDateTime = deviceClientSteupDateTime;
        this.deviceClientWorkgroup = deviceClientWorkgroup;
        this.fcConnectionStr = fcConnectionStr;
        this.iscsiConnectionStr = iscsiConnectionStr;
        this.offlineTime = offlineTime;
        this.deviceSortIndex = deviceSortIndex;
        this.deviceRemark = deviceRemark;
        this.deviceStandbyModel = deviceStandbyModel;
        this.createTime = createTime;
        this.updateTime = updateTime;
        this.deviceDescription = deviceDescription;
        this.devicType = devicType;
        this.deviceIsNew = deviceIsNew;
        this.clSqS = clSqS;
        this.deviceClientUpgradeState = deviceClientUpgradeState;
        this.deviceClusterIdentity = deviceClusterIdentity;
        this.deviceHardInfo = deviceHardInfo;
        this.devicePort = devicePort;
        this.concurrencyCount = concurrencyCount;
        this.deviceInstallType = deviceInstallType;
        this.targetclientUrl = targetclientUrl;
        this.deviceInstallId = deviceInstallId;
    }


    public static enum DeviceState{
        NEW_REGISTER(1, "新加入"),
        ON_LINE(1, "新加入"),
        OFF_LINE(1, "离线"),
        UN_INSTALL(999, "已卸载");

        private Integer state;

        private String desc;

        DeviceState(Integer state, String desc) {
            this.state = state;
            this.desc = desc;
        }

        DeviceState(Integer state) {
            this.state = state;
        }
    }

    /**
     * 新设备注册
     */
    public void register(SourceDeviceDomainService sourceDeviceDomainService) throws ValidationException{
        if(sourceDeviceDomainService.sourceHasTask(this)){
            throw new ValidationException("源设备已经存在，不能重复注册");
        }else {
            this.deviceState = DeviceState.NEW_REGISTER.state;
        }
    }

    /**
     * 设备上线
     */
    public void onLine(){
        this.deviceState = DeviceState.ON_LINE.state;
    }

    /**
     * 设备离线
     */
    public void offLine(){
        this.deviceState = DeviceState.OFF_LINE.state;
    }

    /**
     * 卸载设备
     * @param
     * @throws ValidationException
     */
    public void uninstall(){

        this.deviceState = DeviceState.UN_INSTALL.state;
    }


    public void uninstallDesc(String result){
        //卸载结果保存
    }

    /**
     * 获取源设备所属状态
     * diskClone作为参数传入，不可改变
     * @param sourceDeviceDomainService
     */
    public int getSourceState(SourceDeviceDomainService sourceDeviceDomainService){
        return sourceDeviceDomainService.getSourceState(this);
    }

    public boolean sourceFilter(String inputState){
        /*//全部
        if (ALL_SOURCE_DEVICE.equals(state)) {
            AllList.add(sourceDeviceView);
            //可用
        } else if (FREE_SOURCE_DEVICE.equals(state) && sourceDeviceView.getState().equals("1")) {
            AllList.add(sourceDeviceView);
        }

        //迁移中
        else if (PAIRED_SOURCE_DEVICE.equals(state) && sourceDeviceView.getState().equals("2")) {
            AllList.add(sourceDeviceView);
        }

        //未授权
        else if (UNAUTHORIZED_SOURCE_DEVICE.equals(state) && sourceDeviceView.getState().equals("0")) {
            AllList.add(sourceDeviceView);
        }

        //不可用  离线和版本不匹配
        else if (NOTUSE_SOURCE_DEVICE.equals(state) && (sourceDeviceView.getState().equals("4") || sourceDeviceView.getState().equals("5"))) {
            AllList.add(sourceDeviceView);
        }

        //已交割
        else if (DELIVERY_SOURCE_DEVICE.equals(state) && (sourceDeviceView.getState().equals("7"))) {
            AllList.add(sourceDeviceView);
        }

        //待迁移
        else if (MSState.UNSTART.val().equals(state) && (sourceDeviceView.getState().equals("8")
                || sourceDeviceView.getState().equals("-1") || sourceDeviceView.getState().equals("6"))){
            AllList.add(sourceDeviceView);
        }

        //待交割
        else if (MSState.UNCUTOVER.val().equals(state) && (sourceDeviceView.getState().equals("9"))) {
            AllList.add(sourceDeviceView);
        }*/

        return true;
    }

    /**
     * 判断是否有绑定任务
     * @param deviceDomainService
     * @return
     */
    public boolean sourceHasTask(SourceDeviceDomainService deviceDomainService){
        return deviceDomainService.sourceHasTask(this);
    }


    /**
     * 获取源设备绑定迁移任务名称和id
     * @param deviceDomainService
     * @return
     */
    public String getSourceTaskName(SourceDeviceDomainService deviceDomainService){
        return deviceDomainService.getSourceTaskName(this);
    }

    /**
     * 获取目标端设备类型
     * 1 云平台设备； 2 裸金属设备； 3 手动安装
     * @return
     */
    public Integer getTargetType(){


        return 0;
    }

    /**
     * 获取目标设备状态
     * @return
     */
    public Integer getTargetState(){

        /*MSVm msVm = bareMetaDeviceGroup.getMsVm();
        CloudDevice cloudDevice = bareMetaDeviceGroup.getCloudDevice();
        BareMetaDevice bareMetaDevice = bareMetaDeviceGroup.getBareMetaDevice();
        Device device = bareMetaDeviceGroup.getDevice();

        Integer installState = null;

        if (msVm != null && msVm.getVmState() != MSVmState.RUNNING) {
            //云主机未处于运行状态
            return "8";
        }

        if (cloudDevice != null || bareMetaDevice != null) {

            if (cloudDevice != null) {
                installState = cloudDevice.getInstallState();
            }
            if (bareMetaDevice != null) {
                installState = bareMetaDevice.getInstallState();
            }

            switch (installState) {
                // 安装成功
                case 1:
                    return getDeviceState(device, sourceDevice,"1");
                // 目标设备程序安装失败
                case 2:
                    return "2";
                // 正在安装目标设备程序
                case 3:
                    return "3";
                // 正在卸载目标设备程序
                case 4:
                    return "4";
                //未知错误
                default:
                    return "0";
            }
        } else { //如果安装过程表都为空 可能为手动安装或未安装的目标端的云主机
            return getDeviceState(device,sourceDevice,"");
        }*/

        return 0;
    }


    /**
     * 根据状态过滤目标设备
     * @return
     */
    public boolean targetDeviceFilter(Integer inputState){
        /*//空闲  可用
        if (MSState.USE.val().equals(deviceState)) {
            return MSState.FREE.val();
        }
        //有任务 以匹配 迁移中 已交割
        if (MSState.TMATCHING.val().equals(deviceState)
                || MSState.TMOVING.val().equals(deviceState)
                || MSState.TDELIVERY.val().equals(deviceState)) {
            return MSState.PAIRED.val();
        }
        //不可用
        if (!MSState.USE.val().equals(deviceState) && !MSState.TMATCHING.val().equals(deviceState)
                && !MSState.TMOVING.val().equals(deviceState) && !MSState.TDELIVERY.val().equals(deviceState)){
            return MSState.TNOTUSE.val();
        }*/

        return true;
    }


}
